import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users'
import Rights from '../components/power/Rights'
import Roles from '../components/power/Roles'
import Cate from '../components/goods/Cate'
import Params from '../components/goods/Params'
import GoodsList from '../components/goods/List'
import Add from '../components/goods/add/Add'
import Order from '../components/order/Order'
import Report from '../components/report/Report'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      // 用户行列
      {
        path: '/users',
        component: Users
      },
      // 角色行列
      {
        path: '/roles',
        component: Roles
      },
      // 权限行列
      {
        path:'/rights',
        component:Rights
      },
      // 商品分类
      {
        path: '/categories',
        component: Cate
      },
      // 商品参数
      {
        path: '/params',
        component: Params
      },
      //商品列表
      {
        path:'/goods',
        component: GoodsList,
      },
      //商品组件
      {
        path: '/goods/add',
        component: Add
      },
      {
        path:'/orders',
        component: Order
      },
      {
        path:'/reports',
        component: Report
      }
    ]
  },


]

const router = new VueRouter({
  routes,
  mode: 'history'
})

// 挂载路由导航守卫 token
router.beforeEach((to, from, next) => {
  // to将访问的路径
  // from 代表重哪个路径跳转而来
  // next 是应该函数 表示放行
  // next('login') 强制跳转

  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')

  if (!tokenStr) return next('/login')
  next()

})


export default router
